package cn.xspace.library.view.nav;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import cn.xspace.library.R;
import cn.xspace.library.util.DrawableUtils;


/**
 * Created by JuQiu
 * on 16/8/18.
 */
public class NavigationButton extends FrameLayout {
    private Fragment mFragment = null;
    private Class<?> mClx;
    private ImageView mIconView;
    private TextView mTitleView;
    private TextView mDot;
    private String mTag;
    private String mUrl;

    public NavigationButton(Context context) {
        super(context);
        init();
    }

    public NavigationButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NavigationButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public NavigationButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.layout_nav_item, this, true);

        mIconView = (ImageView) findViewById(R.id.nav_iv_icon);
        mTitleView = (TextView) findViewById(R.id.nav_tv_title);
        mDot = (TextView) findViewById(R.id.nav_tv_dot);
    }

    public void setSelected(boolean selected) {
        super.setSelected(selected);
        mIconView.setSelected(selected);
        mTitleView.setSelected(selected);
    }

    public void showRedDot(int count) {
        mDot.setVisibility(count > 0 ? VISIBLE : GONE);
        mDot.setText(String.valueOf(count));
    }

    public void init(@DrawableRes int resId, @StringRes int strId, Class<?> clx) {
        mIconView.setImageResource(resId);
        mTitleView.setText(strId);
        mClx = clx;
        mTag = mClx.getName();
    }

    public void init(@DrawableRes int resId, String str, Class<?> clx) {
        mIconView.setImageResource(resId);
        mTitleView.setText(str);
        mClx = clx;
        mTag = mClx.getName();
    }

    public void init(String resName, String resSeletedName, String str, Class<?> clx) {
//        try {
//            BitmapFactory.decodeStream(BaseAppliction.mInstance.getAssets().open(BuildConfig.ASSENT_ICONS_NAME + resName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//        mIconView.setImageDrawable(
//                DrawableUtils.setSeletedState(
//                        DrawableUtils.getDrawablepbyAssentsName(resName),
//                        DrawableUtils.getDrawablepbyAssentsName(resSeletedName)
//                ));
//        mIconView.setImageResource();
//        mTitleView.setText(str);
//        mClx = clx;
//        mTag = mClx.getName();
    }

    public void setmUrl(String mrl) {
        this.mUrl = mrl;
    }

    public Class<?> getClx() {
        return mClx;
    }

    public Fragment getFragment() {
        return mFragment;
    }

    public void setFragment(Fragment fragment) {
        this.mFragment = fragment;
    }



    public String getmUrl() {
        return mUrl;
    }
}
